# -*- coding: utf-8 -*-

import logging

class LoggerHandler(object):
    def __get__(self, instance, owner):
        return logging.getLogger('%s.%s' % ( owner.__module__ , owner.__name__) )

class ModelWrapper(object):
    
    _objects = None
    
    def __init__(self, model):
        self._model = model
        self._model_class = model.__class__
        self.name = model.__class__.__name__
    
    def _get_objects(self):
        from autoadmin.model import Session

        self._objects = Session.query(self._model_class).filter_by().all()
        
        return self._objects or []
    
    objects = property(_get_objects)
    
def load_object(module_path):
    
    from alchemistkit.exceptions import ImproperlyConfigured
    
    """
        HINT: django-based/like objects loading
        
        Загружает объект доступный по `module_path`.
        `module_path` должен иметь вид понятный импортеру,
        например 'lib.hooks.action'.
        
        raises:
            ImproperlyConfigured
    """
    try:
        dot = module_path.rindex('.')
    except ValueError:
        raise ImproperlyConfigured, "Isn't a `%s` module" % (module_path, )
                
    modulename, objectname  = module_path[:dot], module_path[dot+1:]
            
    try:
        mod = __import__(modulename, {}, {}, [objectname])
    except ImportError, e:
        raise ImproperlyConfigured, e
            
    try:
        object = getattr(mod, objectname)
    except AttributeError:
        raise ImproperlyConfigured, 'Object "%s" does not define in "%s" module' % \
                (objectname, modulename)

    return object
